package com.gardeneaters.game_v0.playingfield;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.CanvasGradient;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.canvas.dom.client.CssColor;
import com.google.gwt.dom.client.CanvasElement;

// Create Canvas and fill it with required object/shape
public class Draw {

	private static Canvas redMushroom=null;
	private static Canvas blueMushroom=null;
	private static Canvas greenMushroom=null;
	private static int width,height;
	
	private Draw(){
		
	}
	
	
	public static Canvas redMushroom(int _width, int _height){
		if(width!=_width || height!=_height)
		{
			redMushroom = null;
			width = _width;
			height = _height;
		}
		if(redMushroom!=null)
			return redMushroom;
		
		
		Canvas canvas = Canvas.createIfSupported();
	    canvas.setCoordinateSpaceWidth(width);
	    canvas.setCoordinateSpaceHeight(height);
	    Context2d context = canvas.getContext2d();
	    
  	  	// Draw Mushroom stem
		drawStem(context);
	    
		//Now draw Muhsroom Head
		CanvasGradient canvasGradient = context.createLinearGradient(0,height/2,width,height/2);
	    canvasGradient.addColorStop(0, "rgba(255, 0, 0, 1)");
	    canvasGradient.addColorStop(0.8, "rgba(200,50,50, 1)");
	    canvasGradient.addColorStop(1, "rgba(255,50,50,1)");
		drawHead(context, canvasGradient);
	      
	    redMushroom = canvas;
	    return redMushroom;
		
	}
	
	public static Canvas blueMushroom(int _width, int _height){
		if(width!=_width || height!=_height)
		{
			blueMushroom = null;
			width = _width;
			height = _height;
		}
		if(blueMushroom!=null)
			return blueMushroom;
		
		
		Canvas canvas = Canvas.createIfSupported();
	    canvas.setCoordinateSpaceWidth(width);
	    canvas.setCoordinateSpaceHeight(height);
	    Context2d context = canvas.getContext2d();
	    
  	  	// Draw Mushroom stem
		drawStem(context);
	    
		//Now draw Muhsroom Head
		CanvasGradient canvasGradient = context.createLinearGradient(0,height/2,width,height/2);
	    canvasGradient.addColorStop(0, "rgba(0, 0, 219, 1)");
	    canvasGradient.addColorStop(0.7, "rgba(50,50,219, 1)");
	    canvasGradient.addColorStop(1, "rgba(0,50,255,1)");
		drawHead(context, canvasGradient);
	      
	    blueMushroom = canvas;
	    return blueMushroom;
		
	}
	
	public static Canvas greenMushroom(int _width, int _height){
		if(width!=_width || height!=_height)
		{
			greenMushroom = null;
			width = _width;
			height = _height;
		}
		if(greenMushroom!=null)
			return greenMushroom;
		
		
		Canvas canvas = Canvas.createIfSupported();
	    canvas.setCoordinateSpaceWidth(width);
	    canvas.setCoordinateSpaceHeight(height);
	    Context2d context = canvas.getContext2d();
	    
  	  	// Draw Mushroom stem
		drawStem(context);
	    
		//Now draw Muhsroom Head
		CanvasGradient canvasGradient = context.createLinearGradient(0,height/2,width,height/2);
	    canvasGradient.addColorStop(0, "rgba(15, 230, 0, 1)");
	    canvasGradient.addColorStop(0.8, "rgba(90,200,70, 1)");
	    canvasGradient.addColorStop(1, "rgba(0,150,70,1)");
		drawHead(context, canvasGradient);
	      
	    greenMushroom = canvas;
	    return greenMushroom;
		
	}
	
	public static Canvas purpleMushroom(int _width, int _height){
		if(width!=_width || height!=_height)
		{
			greenMushroom = null;
			width = _width;
			height = _height;
		}
		if(greenMushroom!=null)
			return greenMushroom;
		
		
		Canvas canvas = Canvas.createIfSupported();
	    canvas.setCoordinateSpaceWidth(width);
	    canvas.setCoordinateSpaceHeight(height);
	    Context2d context = canvas.getContext2d();
	    
  	  	// Draw Mushroom stem
		drawStem(context);
	    
		//Now draw Muhsroom Head
		CanvasGradient canvasGradient = context.createLinearGradient(0,height/2,width,height/2);
	    canvasGradient.addColorStop(0, "rgba(180, 30, 140, 1)");
	    canvasGradient.addColorStop(0.7, "rgba(180,80,190, 1)");
	    canvasGradient.addColorStop(1, "rgba(150,30,190,1)");
		drawHead(context, canvasGradient);
	      
	    greenMushroom = canvas;
	    return greenMushroom;
		
	}
	
	private static void drawHead(Context2d context, CanvasGradient canvasGradient){
		int canvasHeight = height;
		int canvasWidth = width;

		context.setFillStyle(canvasGradient);
		context.setStrokeStyle(canvasGradient);
	    context.beginPath();
	    context.moveTo(0, 1.5*canvasHeight/4);
	    context.bezierCurveTo(canvasWidth/2, -1.5*canvasHeight/4, canvasWidth*3/4, 1.5*canvasHeight/4, canvasWidth, 1.5*canvasHeight/4);
	    context.bezierCurveTo(canvasWidth*3/4, 1.5*canvasHeight*3/8, canvasWidth/4, 1.5*canvasHeight*3/16, 0,1.5*canvasHeight/4);
	    context.closePath();  
	    context.stroke();
	    context.fill();
	}
	
	private static void drawStem(Context2d context){
		
		int canvasHeight = height;
		int canvasWidth = width;
        CanvasGradient canvasGradient = context.createLinearGradient(0,canvasHeight/2,canvasWidth,canvasHeight/2);
        canvasGradient.addColorStop(0, "rgba(255, 255, 0, 1)");
        canvasGradient.addColorStop(0.6, "rgba(200,200,0, 1)");
        canvasGradient.addColorStop(1, "rgba(0,0,0,1)");
        

		context.setFillStyle(canvasGradient);
		context.setStrokeStyle(canvasGradient);
		context.beginPath();
	     
	      context.moveTo(canvasWidth*3/8, canvasHeight/4);
	      context.bezierCurveTo(canvasWidth*3/8, canvasHeight/4, canvasWidth*3/8, canvasHeight*7/8, canvasWidth/4, canvasHeight);
	      context.lineTo(canvasHeight*3/4, canvasHeight);
	      
	    context.moveTo(canvasWidth*3/8, canvasHeight/4);
	    context.lineTo(canvasWidth*5/8,canvasHeight/4);
	    context.bezierCurveTo(canvasWidth*5/8, canvasHeight/4, canvasWidth*5/8, canvasHeight*7/8, canvasHeight*3/4, canvasHeight);
	    
	      context.closePath();
	    context.stroke();
	    context.fill();
	}
	
	public static CanvasElement arrow(int width, int height){
		Canvas canvas = Canvas.createIfSupported();
	    canvas.setCoordinateSpaceWidth(width);
	    canvas.setCoordinateSpaceHeight(height);
	    Context2d context = canvas.getContext2d();
		
		int canvasHeight = height;
		int canvasWidth = width;
		
        CanvasGradient canvasGradient = context.createLinearGradient(0,canvasHeight/2,canvasWidth,canvasHeight/2);
        canvasGradient.addColorStop(0, "rgba(255, 200, 0, 1)");
        canvasGradient.addColorStop(1, "rgba(200,20,120, 1)");
		context.setFillStyle(canvasGradient);
		context.setStrokeStyle(canvasGradient);
		
		context.beginPath();
		context.moveTo(0 , canvasHeight/3);
		context.lineTo(canvasWidth*2/3, canvasHeight/3);
		context.lineTo(canvasWidth*2/3, 0);
		context.lineTo(canvasWidth, canvasHeight/2);
		context.lineTo(canvasWidth*2/3, canvasHeight);
		context.lineTo(canvasWidth*2/3, canvasHeight*2/3);
		context.lineTo(0, canvasHeight*2/3);
		context.lineTo(0, canvasHeight/3);
		context.closePath();
		context.stroke();
		context.fill();
		
		return canvas.getCanvasElement();
	}
}
